﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace testCpuUsageCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            MakeCpuUsage(50);
        }

        static void MakeCpuUsage(float level)
        {
            PerformanceCounter perfCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");

            while (true)
            {
                float nextValue = perfCounter.NextValue();
                if (nextValue > level)
                {
                    System.Threading.Thread.Sleep(10);
                }

//                 System.Threading.Thread.Sleep(1000); // wait for 1 second 
//                 float cpuLoad = perfCounter.NextValue();
//                 Console.WriteLine("CPU load = " + cpuLoad + " %.");
            }
        }

    }
}
